package whf.framework.web.tag;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import whf.framework.i18n.ApplicationResource;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.Constants;
import whf.framework.web.PageMessages;
import whf.framework.web.tag.decorator.MessageDecorator;

/**
 * @author wanghaifeng
 * @create Aug 31, 2006 10:50:02 PM
 * 
 */
public class MessageTag extends HtmlTag{
	private static Log log = LogFactory.getLog(MessageTag.class);
	
	private MessageDecorator defaultMsgDecorator = new ToStringMessageDecorator();
	
	private Map<Class, MessageDecorator> decorators = Utils.newHashMap();
	
	private String msgTableClass = "msg_table";
	
	private String msgRowClass = "msg_row";
	
	private String msgCellClass = "msg_cell";
	
	/**
	 * @param msgRowClass The msgRowClass to set.
	 */
	public void setMsgRowClass(String msgRowClass) {
		this.msgRowClass = msgRowClass;
	}
	/**
	 * @param msgCellClass The msgCellClass to set.
	 */
	public void setMsgCellClass(String msgCellClass) {
		this.msgCellClass = msgCellClass;
	}
	/**
	 * @param msgTableClass The msgTableClass to set.
	 */
	public void setMsgTableClass(String msgTableClass) {
		this.msgTableClass = msgTableClass;
	}
	
	/**
	 * @param decorators The decorators to set.
	 */
	public void setDecorators(Map<Class, MessageDecorator> decorators) {
		this.decorators = decorators;
	}
	/**
	 * @param defaultMsgDecorator The defaultMsgDecorator to set.
	 */
	public void setDefaultMsgDecorator(MessageDecorator defaultMsgDecorator) {
		if(defaultMsgDecorator == null) return;
		this.defaultMsgDecorator = defaultMsgDecorator;
	}
	
	private String getMessage(String key){
		String msg = (String)pageContext.getAttribute(key);
		if(StringUtils.isEmpty(msg)){
			msg = (String)request().getAttribute(key);
		}
		if(StringUtils.isEmpty(msg)){
			msg = (String)request().getSession().getAttribute(key);
		}
		return msg;
	}
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		String msg = this.getMessage(Constants.MESSAGE_THEN_CLOSE);
		PageMessages pageMessages = (PageMessages)super.request().getAttribute(Constants.PAGE_MESSAGE_OBJECTS);
		if(!StringUtils.isEmpty(msg)){
			if(msg.startsWith("i18n.")) {
				msg = msg.substring(5);
				String tmp = ApplicationResource.get(msg);
				if(tmp != null) msg = tmp;
			}
			super.println("<script>");
			super.print("alert('"+msg+"');");
			super.print("window.close();");
			super.print("</script>");
		} else if(pageMessages != null && !pageMessages.isEmpty()){
			List objs = pageMessages.getMessageObjects();
			try{
				super.print("<table class='").print(this.msgTableClass).print("'").print(">");
				int rowCount = 0;
				for(Iterator it = objs.iterator(); it.hasNext(); ){
					Object o = it.next();
					if(o == null) continue;
					rowCount ++;
					MessageDecorator dec = this.decorators.get(o.getClass());
					if(dec == null) dec = this.defaultMsgDecorator;
					super.println("<tr class='" + this.msgRowClass).print("'>");
					super.println("<td width='5%'>").print(rowCount).print("</td>");
					super.println("<td width='*%' class='").print(this.msgCellClass).print("'>").print(dec.decoorate(o)).print("</td>");
					super.println("</tr>");
				}
				super.println("</table>");
			}catch(Exception e){
				log.warn(e);
			}
		}
		super.release();
		return SKIP_BODY;
	}
	
	class ToStringMessageDecorator implements MessageDecorator{

		/* (non-Javadoc)
		 * @see whf.framework.web.tag.decorator.MessageDecorator#decoorate(java.lang.Object)
		 */
		public String decoorate(Object input) {
			if(input != null){
				return input.toString();
			} else {
				return "";
			}
		}
		
	}
}
